import Poster from '../../../components/poster/poster'
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'
import base from '../../../utils/base64src'
import storage from "../../../utils/storage.js"
const app = getApp();
const ajax = app.myRequest()
var date = new Date();
date = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
var posterConfig = {
    width: 750,
    height: 850,
    backgroundColor: '#fff',
    debug: false,
    pixelRatio: 1,
    preload: 'true',
    hideLoading: 'true',
    commentText: '',
    texts: [
        {
            x: 50,
            y: 550,
            width: 350,
            baseLine: 'top',
            text: 'ewre2w',
            fontSize: 32,
            color: 'rgba(116,84,254,1)',
        },
        {
            x: 50,
            y: 600,
            width: 350,
            baseLine: 'top',
            text: 'ewreqrq',
            fontSize: 32,
            color: '#000',
        },
        {
            x: 50,
            y: 700,
            width: 350,
            baseLine: 'middle',
            lineNum: 4,
            lineHeight: 50,
            text: 'erwqw',
            fontSize: 26,
            color: 'rgba(116,84,254,1)',
        },
        {
            x: 535,
            y: 780,
            width: 200,
            baseLine: 'middle',
            lineNum: 2,
            lineHeight: 40,
            text: date,
            fontSize: 26,
            color: '#000',
        }
    ],
    images: [
        {
            width: 650,
            height: 450,
            x: 50,
            y: 50,
            borderRadius: 40,
            url: 'https://www.yuehanme.com/uploads/20200913/64346cec0688ebcf5a455640b963820b.jpg',
        },
        {
            width: 200,
            height: 200,
            x: 500,
            y: 530,
            url: '/images/back.png',
        }
    ]
}

Page({

    data: {
        posterConfig: posterConfig,
        images: [
            '/images/coach.png',
            '/images/coach.png',
            '/images/coach.png'
        ],
        indicatorDots: true,
        autoplay: true,
        interval: 5000,
        duration: 1000,
        id: '',
        latitude: '',
        longitude: '',
        hot: '',
        show: false,
        reward: false,
        friend: false,
        is_like: false,
        is_fabulous: false,
        commentShow: false,
        value: 2,
        ggautoplay: true
    },

    onLoad: function (options) {
        let that = this
        if (options.user_id) {
            storage.setStorage('gymnasium_id', options.id)
            storage.setStorage('theElderlyId', options.user_id)
            this.setData({
                id: options.id
            })
        } else if (options.index) {
            var index = options.index
            this.setData({ id: index })
        } else if (options.scene) {
            const scene = decodeURIComponent(options.scene)
            let id = scene.split("&amp;")[0];
            let theElderlyId = scene.split('&amp;')[1]
            storage.setStorage('theElderlyId', theElderlyId)
            storage.setStorage('gymnasium_id', id)
            this.setData({
                id
            })
        } else {
            this.setData({
                id: options.id,
                latitude: options.latitude,
                longitude: options.longitude
            })
        }
        this.getVenue() // 场馆信息
        this.getCoach() // 教练
        this.getMember() // 会员卡
        this.getWellKnownSaying() // 分享的名人名言
        this.getCode()
        ajax.getRequest('/User/profile').then((res) => {
            that.setData({ user: res.data.data })
        })
    },

    ping() {
        ajax.postRequest('/Gymnasium/evaluate', { gymnasium_id: 6, evaluate: 'afghjkldtfyguhkjlwrfe', values: 2 }).then((res) => {
        })
    },


    // 播放视频 
    videoPlay() {
        setTimeout(function () {
            var videoContext = wx.createVideoContext('myVitemideo')
            videoContext.play()
        }, 200)
        this.setData({ ggautoplay: false })
    },

    // 滑动
    bindchange: function (e) {
        this.setData({ _index: e.detail.current, ggautoplay: true })
    },

    // 获取场馆信息 
    getVenue() {
        var latitude = this.data.latitude
        var longitude = this.data.longitude
        var id = this.data.id
        var _this = this
        ajax.getRequest('/Gymnasium/index', { latitude, longitude, id }).then((res) => {
            if (res.data.code == 0) {
                Dialog.alert({
                    message: '暂无该场馆信息',
                }).then(() => {
                    wx.navigateBack()
                })
            } else {
                _this.setData({
                    hot: res.data.data,
                    is_like: res.data.data.is_like,
                    is_fabulous: res.data.data.is_fabulous,
                    commentInformation: res.data.data.evaluate
                })
                posterConfig.texts[0].text = res.data.data.name + res.data.data.type_text
                posterConfig.texts[1].text = res.data.data.address
                if (typeof (res.data.data.share_images) == 'string') {
                    posterConfig.images[0].url = res.data.data.share_images
                } else if (Array.isArray(res.data.data.share_images)) {
                    var index = Math.floor((Math.random() * res.data.data.share_images.length));
                    posterConfig.images[0].url = res.data.data.share_images[index]
                } else {
                    posterConfig.images[0].url = res.data.data.logo_image
                }
                if (typeof (res.data.data.images) == 'string') {
                    this.setData({ VenueStringImages: res.data.data.images })
                } else {
                    this.setData({ VenueArrayImages: res.data.data.images })
                }
                wx.hideLoading();
                wx.setNavigationBarTitle({
                    title: res.data.data.name
                })
            }
        })

    },

    ddd() {
        var a = this.data.getmember[1].price * 3
    },

    // 分享
    share() {
        var _this = this
        wx.getSetting({
            success: function (res) {
                if (res.authSetting['scope.userInfo']) {
                    _this.setData({ show: true });
                } else {
                    wx.navigateTo({
                        url: '/pages/login/SignIn/index',
                    })
                }
            }
        })

    },

    onClose() {
        this.setData({ show: false });
    },


    // 评论
    comment() {
        var _this = this
        wx.getSetting({
            success: function (res) {
                if (res.authSetting['scope.userInfo']) {
                    _this.setData({ commentShow: true })
                } else {
                    wx.navigateTo({
                        url: '/pages/login/SignIn/index',
                    })
                }
            }
        })
    },

    onCloseComment() {
        this.setData({ commentShow: false, commentText: '', value: 2 })
    },

    onChange(event) {
        this.setData({
            value: event.detail,
        });
    },

    bindFormSubmit: function (e) {
        var evaluate = e.detail.value.textarea
        var values = this.data.value
        var gymnasium_id = this.data.id
        var _this = this
        if (evaluate == '') {
            Dialog.alert({ message: '请填写评论' })
        } else {
            ajax.postRequest('/Gymnasium/evaluate', { evaluate, values, gymnasium_id }).then((res) => {
                Dialog.alert({ message: '发表评论成功' }).then(() => {
                    _this.setData({ commentShow: false, commentText: '', value: 2 })
                    _this.getVenue()
                })
            })
        }
    },

    // 教练列表 comment
    getCoach() {
        var id = this.data.id
        var _this = this
        ajax.getRequest('/Coach/index', { gymnasium_id: id }).then((res) => {
            _this.setData({
                getcoach: res.data.data.data,
                current_page: res.data.data.current_page
            })
            wx.hideLoading();
        })
    },

    // 会员卡列表
    getMember() {
        var id = this.data.id
        var _this = this
        ajax.getRequest('/Good/index', { gymnasium_id: id }).then((res) => {
            _this.setData({
                getmember: res.data.data.data,
                originalPrice: Number(res.data.data.data.price * 3)
            })
            wx.hideLoading();
        })
    },

    // 跳到教练页
    toCoach(e) {
        var gymnasium_id = this.data.id
        var index = e.currentTarget.dataset.idx
        var coach_id = this.data.getcoach[index].id
        wx.navigateTo({
            url: '/pages/venue/coach/index?coach_id=' + coach_id + '&gymnasium_id=' + gymnasium_id,
        })
    },

    closeReward() {
        this.setData({ reward: false })
    },
    // 跳到会员卡页
    toMember(e) {
        var index = e.currentTarget.dataset.index
        var id = this.data.getmember[index].id
        wx.navigateTo({ url: '/pages/venue/member/index?id=' + id })

    },

    // 点赞
    spotFabulous() {
        var id = this.data.id
        var _this = this
        var is_fabulous = this.data.is_fabulous
        wx.getSetting({
            success: function (res) {
                if (res.authSetting['scope.userInfo']) {
                    ajax.getRequest('/Gymnasium/likes', { id }).then((res) => {
                        _this.setData({ is_fabulous: !is_fabulous })
                        var likes = 0
                        if (_this.data.is_fabulous) { likes++ } else { likes-- }
                        _this.setData({ 'hot.likes': _this.data.hot.likes + likes })
                        wx.showToast({ title: res.data.msg, icon: 'none', duration: 1000 })
                    })
                } else {
                    wx.navigateTo({ url: '/pages/login/SignIn/index' })
                }
            }
        })
    },

    // 点击收藏
    setCollection() {
        var id = this.data.id
        var _this = this;
        var is_like = _this.data.is_like
        wx.getSetting({
            success: function (res) {
                if (res.authSetting['scope.userInfo']) {
                    ajax.getRequest('/User/like', { gymnasium_id: id, type: 1 }).then((res) => {
                        _this.setData({ is_like: !is_like })
                        var likes = 0
                        if (_this.data.is_like) { likes++ } else { likes-- }
                        _this.setData({ 'hot.collections': _this.data.hot.collections + likes })
                        wx.showToast({ title: res.data.msg, icon: 'none', duration: 1000 })
                    })
                } else {
                    wx.navigateTo({ url: '/pages/login/SignIn/index' })
                }
            }
        })
    },

    // 拨打电话
    MakeAcall() {
        var phoneNumber = this.data.hot.phone
        wx.makePhoneCall({ phoneNumber: phoneNumber })
    },

    // 生成海报 

    friend() {
        this.getWellKnownSaying()
        this.setData({ friend: true, show: false, posterConfig }, () => { Poster.create(true) })
    },

    // 保存图片
    preservation() {
        var detail = this.data.poster
        var _this = this
        wx.saveImageToPhotosAlbum({
            filePath: detail,
            success(res) {
                _this.setData({ friend: false })
                if (res.errMsg) {
                    wx.showToast({ title: '图片保存成功,请分享到朋友圈', icon: 'none', duration: 3000 })
                }
            }
        })
    },

    onPosterSuccess(e) {
        this.setData({ poster: e.detail })
    },

    getWellKnownSaying() {
        ajax.getRequest('/content/inspirational').then((res) => { posterConfig.texts[2].text = res.data.data })
    },

    // 关闭海报
    closeFriend() {
        this.setData({ friend: false })
    },

    // 右化分页
    paging(e) {
        var _this = this
        var id = _this.data.id
        var current_page = _this.data.current_page
        ajax.getRequest('/Coach/index', { gymnasium_id: id, page: current_page + 1 }).then((res) => {
            var getcoach = _this.data.getcoach.concat(res.data.data.data)
            _this.setData({
                getcoach: getcoach,
                current_page: res.data.data.current_page,
                total: res.data.data.total
            })
            wx.hideLoading();
        })
    },

    // 地图导航
    goVenue() {
        var latitude = this.data.hot.latitude
        var longitude = this.data.hot.longitude
        var address = this.data.hot.address
        wx.openLocation({ latitude: Number(latitude), longitude: Number(longitude), scale: 18, address: address })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        var _this = this
        var id = this.data.id
        var name = this.data.hot.name + this.data.hot.type_text
        var hot = this.data.hot
        if (typeof (hot.share_images) == 'string') {
            var images = this.data.hot.share_images
        } else if (Array.isArray(hot.share_images)) {
            var index = Math.floor((Math.random() * hot.share_images.length));
            var images = this.data.hot.share_images[index]
        }
        var user_id = wx.getStorageSync('user_id') || '';
        return {
            title: name,
            imageUrl: images,
            path: "/pages/venue/venueHome/index?id=" + id + '&user_id=' + user_id,
            success: function (res) {
                wx.showToast({ title: '分享成功', mask: true, icon: 'success', duration: 2000 })
                _this.setData({ show: false })
            },
        }
    },

    // 生成场馆二维码
    getCode() {
        var page = 'pages/venue/venueHome/index'
        var id = this.data.id
        var user_id = wx.getStorageSync('user_id');
        var scene = String(id) + '&' + user_id
        var width = 300
        var _this = this
        ajax.postRequest('/Content/qrcode', { page, scene, width }).then((res) => {
            const arrayBuffer = base(res.data.data)
            arrayBuffer.then((res) => {
                _this.setData({ img: res })
                posterConfig.images[1].url = res
            })
        })
    },

    // 出示三日卡二维码
    dianjisanrika: function () {
        let that = this
        if (that.data.shareCode_ == null) {
            var page = 'pages/personal/systemSettings/index'
            var scene = that.data.user.reward.id + '&' + 2
            var width = 300
            ajax.postRequest('/Content/qrcode', { page, scene, width }).then((res) => {
                if (res.data.code == 1) {
                    that.setData({ shareCode_: res.data.data, shareCode: !0 })
                } else {
                    wx.showToast({ title: 'ServerError：' + res.data.msg, icon: 'none' })
                }
            })
        } else {
            that.setData({ shareCode: !0 })
        }
    },

    // 关闭三日卡二维码
    closeShareCode: function () {
        this.setData({ shareCode: !1 })
    }
})